﻿/// <reference path="../libs/jquery-1.8.2-vsdoc.js" />
/// <reference path="../dal/sca.dal.sp.js" />

var SCA = SCA || {};
SCA.UI = SCA.UI || {};
SCA.UI.LocationSelection = SCA.UI.LocationSelection || {};

SCA.UI.LocationSelection.Dialog = function (title, siteAbsoluteUrl, treeParams, callback) {
    
    this.title = title;
    this.callback = callback;
    this.siteAbsoluteUrl = siteAbsoluteUrl;
    this.DAL;

    var treeParams = treeParams || {};
    treeParams.allowSelectWeb = treeParams.allowSelectWeb || false;
    treeParams.allowSelectList = treeParams.allowSelectList || true;
    treeParams.allowSelectFolder = treeParams.allowSelectFolder || false;
    treeParams.allowSelectItem = treeParams.allowSelectItem || false;
    treeParams.exploreDepth = treeParams.exploreDepth || "List";
    
    this.treeData;
   
    var selectedNode;

    var thisContext = this;

    this.Setup = function () {
        //TODO: cleanup
        jQuery("#ButtonLocationSelectionDialogOk").unbind('click');
        jQuery("#ButtonLocationSelectionDialogCancel").unbind('click');
        jQuery("#ButtonLocationSelectionDialogOk").click(function () {
            var entity = selectedNode.data.key;
            var localCallback = function () {
                thisContext.callback(entity)
            };

            if (entity instanceof SCA.DAL.List) {
                entity.Load(localCallback, localCallback);
            }
            else if (entity instanceof SCA.DAL.Folder) {
                entity.parentList.Load(localCallback, localCallback);
            }

            thisContext.Close();
        });
        jQuery("#ButtonLocationSelectionDialogOk").attr("disabled", "disabled");
        jQuery("#ButtonLocationSelectionDialogCancel").click(function () { thisContext.Close(); });
    }
    
    this.Open = function ()
    {
        this.treeData = {
            title: this.siteAbsoluteUrl, isFolder: true, key: "rootNode", expand: true,
            children: new Array(),
            hideCheckbox: true,
            unselectable: true,
        };

        var root = jQuery("#tree1").dynatree("getRoot");
        root.removeChildren();
        root.addChild(this.treeData);

        this.DAL = new SCA.DAL.SP(this.siteAbsoluteUrl);
        this.DAL.Init(Function.createDelegate(this, this.onSuccess),Function.createDelegate(this, this.onFail));
    }

    this.onSuccess = function()
    {
        var rootNode = jQuery("#tree1").dynatree("getTree").getNodeByKey("rootNode");
        this.AddTreeChildren(rootNode, this.DAL.rootWeb.websCollection, this.DAL.rootWeb.listsCollection);

        jQuery("#tree1").dynatree("option", "onSelect", function (select, node) {
            selectedNode = node;
            jQuery("#ButtonLocationSelectionDialogOk").removeAttr("disabled");
        });

        jQuery("#tree1").dynatree("option", "onLazyRead", function (node) {
            //debugger;
            var entity = node.data.key;
            if (entity instanceof SCA.DAL.Web) {
                entity.Load(function () { thisContext.AddTreeChildren(node, entity.websCollection, entity.listsCollection); }, this.onFail);
            }
            else if (entity instanceof SCA.DAL.List) {
                entity.rootFolder.Load(function () { thisContext.AddFolderChildrenToTree(node, entity.rootFolder.subFolderAndItemCollection); }, this.onFail);
            }
            else if (entity instanceof SCA.DAL.Folder) {
                entity.Load(function () { thisContext.AddFolderChildrenToTree(node, entity.subFolderAndItemCollection); }, this.onFail);
            }

        });

        jQuery("#LocationSelectionDialog").dialog({ title: this.title, width: "600px" });
    }

    this.Close = function()
    {
        jQuery("#LocationSelectionDialog").dialog("close");
    }

    this.AddTreeChildren = function (node, websCollection, listsCollection) {
        var children = new Array();
        for (var i = 0 ; i < websCollection.length; i++) {
            var current = websCollection[i];
            var treeNode = {
                title: current.title,
                key: current,
                expand: false,
                hideCheckbox: !treeParams.allowSelectWeb,
                unselectable: !treeParams.allowSelectWeb,
                isLazy: (treeParams.exploreDepth == "List" || treeParams.exploreDepth == "Folder" || treeParams.exploreDepth == "Item")
            };
            children.push(treeNode);
        }

        for (var i = 0 ; i < listsCollection.length; i++) {
            var current = listsCollection[i];
            var treeNode = {
                title: current.title,
                key: current,
                expand: false,
                hideCheckbox: !treeParams.allowSelectList,
                unselectable: !treeParams.allowSelectList,
                isLazy: (treeParams.exploreDepth == "Folder" || treeParams.exploreDepth == "Item")
            };
            children.push(treeNode);
        }
        
        node.setLazyNodeStatus(DTNodeStatus_Ok);
        node.addChild(children);
    }

    this.AddFolderChildrenToTree = function(node, itemsCollection){
        var children = new Array();
        for (var i = 0 ; i < itemsCollection.length; i++) {
            var current = itemsCollection[i];

            var treeNode = {
                title: current.title,
                key: current,
                expand: false
            };

            if (current instanceof SCA.DAL.Folder)
            {
                treeNode.hideCheckbox = !treeParams.allowSelectFolder;
                treeNode.isLazy = treeParams.exploreDepth == "Folder";
            }
            else
            {
                if (treeParams.exploreDepth != "Item") {
                    continue;
                }
                treeNode.hideCheckbox = !treeParams.allowSelectItem;
                treeNode.isLazy = false;
            }
          
            children.push(treeNode);
        }

        node.setLazyNodeStatus(DTNodeStatus_Ok);
        node.addChild(children);
    }


    this.onFail = function(data)
    {
        //debugger;
        alert("fail:" + data);
    }

    this.Setup();
}